﻿using System;
using System.Diagnostics;

namespace MSPE.Public.FOSS.AutoStatusPlus.Common
{
    internal class EventEntry : IDisposable
    {
        #region Private / Protected
        private const EventLogEntryType _defaultEntryType = EventLogEntryType.Error;
        private const int _defaultEventId = 9681;
        private EventLog _eventLog;
        private EventLogEntryType _entryType;
        private int _eventId;
        #endregion // Private / Protected

        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        public EventEntry(string defaultSourceName)
            :this(_defaultEntryType, defaultSourceName)
        {
        } // EventEntry()

        public EventEntry(EventLogEntryType defaultEntryType, string defaultSourceName)
            : this(defaultEntryType, _defaultEventId, defaultSourceName)
        {
        } // EventEntry()

        public EventEntry(EventLogEntryType defaultEntryType, int defaultEventId, 
            string defaultSourceName)
        {
            _eventLog = new EventLog();
            _entryType = defaultEntryType;
            _eventId = defaultEventId;            
            _eventLog.Source = defaultSourceName;
            _eventLog.EnableRaisingEvents = false;
        } // EventEntry()
        #endregion // Constructors

        #region Methods
        /// <summary>
        /// Write application event
        /// </summary>
        /// <param name="msg">Message</param>
        public void WriteEvent(string msg)
        {
            _eventLog.WriteEntry(msg, _entryType, _eventId);
        } // WriteEvent()

        /// <summary>
        /// Write application event
        /// </summary>
        /// <param name="msg">Message</param>
        /// <param name="logEntryType">Log entry type</param>
        public void WriteEvent(string msg, EventLogEntryType logEntryType)
        {
            _eventLog.WriteEntry(msg, logEntryType, _eventId);
        } // WriteEvent()

        /// <summary>
        /// Write application event
        /// </summary>
        /// <param name="msg">Message</param>
        /// <param name="logEntryType">Log entry type</param>
        /// <param name="eventId">Event Id</param>
        public void WriteEvent(string msg, EventLogEntryType logEntryType, int eventId)
        {
            _eventLog.WriteEntry(msg, logEntryType, eventId);
        } // WriteEvent()
        #endregion // Methods
        
        #region IDisposable Implementation
        /// <summary>
        /// Releases all resources used by an instance of the <see cref="EventEntry" /> class.
        /// </summary>
        public void Dispose()
        {
            _eventLog.Dispose();
        }       
        #endregion // IDispose Implementation
    }
}